/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"os"
	"ssh-connect/service"
	"ssh-connect/ui"

	"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "ssh-connect",
	Short: "ssh 连接管理工具",
	Long: `ssh 连接工具

	ssh-connect 进入交互界面
	host 支持ip+端口，如不指定使用默认22端口
	password 如果想使用秘钥则置空
	privatekey 支持直接输入秘钥或者秘钥文件路径，不支持~相对路径`,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		return nil

	},
	RunE: func(cmd *cobra.Command, args []string) error {
		config, err := service.GetConfig()
		if err != nil {
			return err
		}

		connUI := ui.InitUI(config)
		return connUI.Run()
	},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

func init() {
	// Here you will define your flags and configuration settings.
	// Cobra supports persistent flags, which, if defined here,
	// will be global for your application.

	// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.t.yaml)")

	// Cobra also supports local flags, which will only run
	// when this action is called directly.
	// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
